﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DLM.Domain.Model;
using DLM.Services.Interfaces;
using DLM.Web.ViewModel.Admin;
using DLM.Web.MVC.Extensions;

namespace DLM.Web.MVC.Areas.Management.Controllers
{
    [AdminAuthorize]
    public class UserController : Controller
    {
        private IUserService userService;
        public UserController(IUserService userService)
        {
            this.userService = userService;
        }

        //
        // GET: /Management/User/

        [HttpGet]
        public ActionResult List(int? page)
        {
            var list = this.userService.GetValidateUser(page ?? 0, 15);
            return View(list);
        }

        [HttpGet]
        public ActionResult BlackList(int? page)
        {
            var list = this.userService.GetBlackList(page ?? 0, 15);
            return View(list);
        }

        [HttpGet]
        public ActionResult Detail(int? id)
        {
            UserInfoPageView userView = new UserInfoPageView();
            userView.User = this.userService.GetUser(id ?? 0);
            userView.Bonus = this.userService.GetBonus(id ?? 0);
            userView.Album = this.userService.GetPhotos(id ?? 0);
            userView.BreakApp = this.userService.GetBreakAppByUser(id ?? 0, 0, 15);
            return View(userView);
        }

        [HttpPost]
        public ActionResult Detail(FormCollection collection)
        {
            try
            {
                var userID = int.Parse(collection["User.UserID"].ToString());
                var accountID = int.Parse(collection["User.AccountID"].ToString());
                this.userService.SetBlack(userID, accountID);
                return RedirectToAction("List", "User");
            }
            catch (Exception ex)
            {

                ModelState.AddModelError("", ex.Message);

            }
            return RedirectToAction("Detail", "User");
        }

        [HttpPost]
        public ActionResult SetInBlack(int userID, int accountID)
        {
            try
            {
                this.userService.SetBlack(userID, accountID);
                return RedirectToAction("List", "User");
            }
            catch (Exception ex)
            {

                ModelState.AddModelError("", ex.Message);

            }
            return RedirectToAction("Detail", "User", new { id = userID });
        }

        [HttpPost]
        public ActionResult Activate(int userID, int accountID)
        {
            this.userService.ActiveUser(userID);
            return RedirectToAction("Detail", "User", new { id = userID });
        }

        [HttpGet]
        public ActionResult Bonus(int? id)
        {
            var bonus = this.userService.GetBonus(id ?? 0);
            return View(bonus);
        }

        [HttpPost]
        public ActionResult UpdateBonus(int id, FormCollection collection)
        {
            var bonuses = this.userService.GetBonus(id);
            bonuses.Bonuses = decimal.Parse(collection["Bonuses"].ToString());
            bonuses.UpdateDate = DateTime.Now;
            this.userService.UpdateBonus(bonuses);
            return View();
        }

        [HttpPost]
        public ActionResult RemovePhoto(int? photoID, int? userID)
        {
            this.userService.RemovePhoto(photoID ?? 0);
            var list = this.userService.GetPhotos(userID ?? 0);
            return View("_Album", list);
        }


        public ActionResult BonusList(int? page)
        {
            var list = this.userService.GetBonusDTO(page ?? 0, 15);
            return View(list);
        }

        [HttpPost]
        public ActionResult BactchUpdateBonus(FormCollection collection, string[] checkedid)
        {
            List<Bonus> bonusList = new List<Bonus>();
            if (checkedid != null)
            {
                foreach (var item in checkedid)
                {
                    Bonus bonus = new Bonus
                    {
                        BonusID = int.Parse(item),
                        Bonuses = decimal.Parse(collection["txtBonus"].ToString()),
                        UpdateDate = DateTime.Now
                    };
                    bonusList.Add(bonus);
                }

            }
            this.userService.UpdateBonus(bonusList);
            var list = this.userService.GetBonusDTO(0, 20);
            return View("BonusList", list);
        }

        [HttpPost]
        public ActionResult SearchUser(FormCollection collection)
        {

            if (collection["txtAccount"] != null && !string.IsNullOrEmpty(collection["txtAccount"]))
            {
                var user = this.userService.GetUser(collection["txtAccount"]);
                List<User> userList = new List<User> { user };
                return View("List", userList);
            }

            if (collection["txtRegStart"] != null && !string.IsNullOrEmpty(collection["txtRegStart"])
                && collection["txtRegEnd"] != null && !string.IsNullOrEmpty(collection["txtRegEnd"]))
            {
                DateTime startTime = DateTime.Parse(collection["txtRegStart"]);
                DateTime endTime = DateTime.Parse(collection["txtRegEnd"]);
                var list = this.userService.GetUser(startTime,endTime);
                return View("List", list);
            }

            return RedirectToAction("List");
        }
    }
}
